 /**
 * 逻辑、功能相关描述: <br>
 *
 * @author 付湘辉  <br>
 * 编写日期：2008-12-30  <br>
 */
package com.ce.pms.occupation.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.service.IChargeRecordProcessor;
import com.ce.pms.charge.service.ITransactionService;
import com.ce.pms.charge.vo.ChargeRecordVO;
import com.ce.pms.charge.vo.CreditTransVO;
import com.ce.pms.charge.vo.DebitTransVO;
import com.ce.pms.charge.vo.PrePaySchChargeVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.occupation.dao.IOccupationChargeItemDao;
import com.ce.pms.occupation.model.OccupationChargeItemDO;
import com.ce.pms.occupation.service.IIdentityValidateService;
import com.ce.util.BlankUtil;

/**
 * 	名称: com.ce.pms.occupation.service.impl.OccupationChargeRecordProcessorImpl <br>
 * 入伙管理非常规费用处理器
 *
 * @author  付湘辉  <br>
 * Created on： 2008-12-30 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class OccupationChargeRecordProcessorImpl extends PmsBaseServiceImpl
		implements IChargeRecordProcessor {

	private IOccupationChargeItemDao occupationChargeItemDao;
	private ITransactionService transactionService;
	private IIdentityValidateService identityValidateService;

	public void setIdentityValidateService(IIdentityValidateService identityValidateService)
	{
		this.identityValidateService = identityValidateService;
	}
	
	public void setTransactionService(ITransactionService transactionService)
	{
		this.transactionService = transactionService;
	}
	public void setOccupationChargeItemDao(IOccupationChargeItemDao occupationChargeItemDao)
	{
		this.occupationChargeItemDao = occupationChargeItemDao;
	}

	/**
	    * 功能描述: 删除入伙费用记录，
	    * 只有入伙预交款业务类型才能删除，
	    * 并且对应的入伙收费项目不是跟进状态的。 <br>
		* @param chargeRecordVO - 费用记录VO
	    *
	    * Create author:付湘辉 <br>
	    * Create on:2008-12-24  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	public Boolean deleteChargeRecord(ChargeRecordVO chargeRecordVO) {
		String bizModule = chargeRecordVO.getBizModule();
		String bizType = chargeRecordVO.getBizType();
		Long bizId = chargeRecordVO.getBizId();
		// 业务模块， 001-装修管理,002-客服事件，003-入伙管理
		if(!SysrefcodeDO.FEE_CHARGE_RECORD_BIZ_MOUDLE_OCC.equals(bizModule)) {
			return false;
		}
		// 业务类型：来自参数配置表的(006 入伙预交款)
		if(!SysrefcodeDO.FEE_CHARGE_RECORD_BIZ_TYPE_OCC_PRE.equals(bizType)){
			return false;
		}
		OccupationChargeItemDO chargeItemDO= occupationChargeItemDao.findById(bizId);
		// 1 跟进的不能删除
		if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(chargeItemDO.getTrackYn())) {
			throw new BusinessException("occupation:occupation_chargeRecordProcessor_deleteCharge_exception", 
																BusinessException.WARNING);
		}

		receiveFreeCharge(bizId);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.IAbstractChargeRecordService#editChargeRecord(com.ce.pms.renovation.vo.ChargeRecordVO)
	 */
	public ChargeRecordVO editChargeRecord(ChargeRecordVO chargeRecord) {
		throw new BusinessException("occupation:occupation_chargeRecordProcessor_editCharge_notSupport_exception", BusinessException.WARNING);
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.IAbstractChargeRecordService#editChargeRecordBatch(java.util.List, java.lang.Long)
	 */
	public List<ChargeRecordVO> editChargeRecordBatch(
			Long operator, List<ChargeRecordVO> chargeRecordVos) {
		throw new BusinessException("occupation:occupation_chargeRecordProcessor_editCharge_notSupport_exception", BusinessException.WARNING);
	}

	/**
	    * 功能描述:处理费用记录  <br>
	    * @param accountHouseId 房屋ID
	    * @param operator 操作者
	    * @param businessCodeId - 支付交易码ID(页面对应收费方式)
	    * @param chargeRecords - 费用记录集
	    * @roseuid 4945E9B200AB
	    * @return Boolean
	    *
	    * Create author:付湘辉 <br>
	    * Create on:2008-12-24  <br>
	    * Edit author: 王浩羽  <br>
	    * Edit on:    <br>
	    * Why: 实现接口    <br>
	    */
	public Boolean handleChargeRecords(Long accountHouseId,
																	 Long operator,
																	 Long businessCodeId,
																	 List<ChargeRecordVO> chargeRecords) {
		List<DebitTransVO> debitTransVOs = new ArrayList<DebitTransVO>();//物业缴费应收VO
		List<CreditTransVO> creditTransVOs = new ArrayList<CreditTransVO>();
		List<PrePaySchChargeVO> prePaySchChargeVOs = new ArrayList<PrePaySchChargeVO>();//预交款缴费VO
		List<OccupationChargeItemDO> chargeItemDOs = new ArrayList<OccupationChargeItemDO>();
		for(ChargeRecordVO chargeRecordVO : chargeRecords) {
//			if(!isAllowHandleOccupationChargeItem(chargeRecordVO.getBizId())) {
//				throw new BusinessException("occupation:occupation_chargeRecordProcessor_handleCharge_exception", BusinessException.WARNING);
//			}
			
			//005 预交款收款 
			if(SysrefcodeDO.FEE_CHARGE_RECORD_OPERATION_TYPE_PREPAY.equals(chargeRecordVO.getOperationType())) {
				PrePaySchChargeVO prePaySchChargeVO = new PrePaySchChargeVO();
				prePaySchChargeVO.setHouseId(accountHouseId);//房间ID
				prePaySchChargeVO.setAccountHouseId(accountHouseId);//房间ID
				prePaySchChargeVO.setBusinessId(businessCodeId);//交易码ID(页面选中的收费方式)
				prePaySchChargeVO.setCredit(chargeRecordVO.getSum());//应收贷方金额
				prePaySchChargeVOs.add(prePaySchChargeVO);
			}
			//004 物业收费
			if(SysrefcodeDO.FEE_CHARGE_RECORD_OPERATION_TYPE_PROPERTY.equals(chargeRecordVO.getOperationType())) {
				//构造应收交易VO
				DebitTransVO debitTransVO = new DebitTransVO();
				debitTransVO.setHouseId(chargeRecordVO.getHouseId());//房间ID
				debitTransVO.setAccountHouseId(accountHouseId);//房间ID
				debitTransVO.setBusinessId(chargeRecordVO.getBusinessId());//抛帐过来的交易码ID
				debitTransVO.setDebit(chargeRecordVO.getSum());//应收借方金额
				debitTransVOs.add(debitTransVO);
				//建立同样数量的实收交易，并放入支付交易码ID
				CreditTransVO creditTransVO = new CreditTransVO();
				creditTransVO.setBusinessId(businessCodeId);
				creditTransVOs.add(creditTransVO);
			}
			
			OccupationChargeItemDO chargeItemDO= occupationChargeItemDao.findById(chargeRecordVO.getBizId());
//			chargeItemDO.setPayState(SysrefcodeDO.OM_FEE_STATE_IN);
//			occupationChargeItemDao.updateObject(chargeItemDO);//改变单个费用的状态
			chargeItemDOs.add(chargeItemDO);
		}
		
		if(!BlankUtil.isBlank(debitTransVOs)) {
			// 物业账号
			transactionService.handleMultiCharge(operator, accountHouseId, debitTransVOs, creditTransVOs);
		}
		
		if(!BlankUtil.isBlank(prePaySchChargeVOs)) {
			// 预交款账号
			transactionService.addPrePayChargeTransactionBatch(operator, accountHouseId, prePaySchChargeVOs);
		}
		
		//费用转移后通知更改每笔收费项目移交账目的状态
		notifyAddedOccupationCharge(chargeItemDOs);
		return true;
	}
	
	/**
	 * 功能描述:通知入伙费用已缴  <br>
	 * @param occupationChargeItemDOs
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 7, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void notifyAddedOccupationCharge(List<OccupationChargeItemDO> occupationChargeItemDOs){
		Long identityValidateId = null;
//		Set<Long> ids = new HashSet<Long>(); 
		for(OccupationChargeItemDO occupationChargeItemDO : occupationChargeItemDOs){
			identityValidateId = occupationChargeItemDao.getIdentityValidateId(occupationChargeItemDO.getKeyword());
//			if(!ids.contains(identityValidateId)){
//				ids.add(identityValidateId);
//			}
			
			receiveAddedOccupationCharge(occupationChargeItemDO.getKeyword(), identityValidateId);
		}

	}
	

	/**
	    * 功能描述: 收到预交款费用免缴通知 （如果财务删除该笔项目费用）<br>
	    * @param occupationChargeItemId
	    * Create author: 王浩羽 <br>
	    * Create on:Jan 7, 2009  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	private void receiveFreeCharge(Long occupationChargeItemId){
		OccupationChargeItemDO occupationChargeItem= occupationChargeItemDao.findById(occupationChargeItemId);
		occupationChargeItem.setPayState(SysrefcodeDO.OM_FEE_STATE_ESC);//设定为免收状态
		occupationChargeItemDao.updateObject(occupationChargeItem);
	}
	
	 /**
	    * 功能描述: 收到预交款费用已缴通知（改明细状态） <br>
	    * @param occupationChargeItemDOId
	    * @param identityValidateId
	    * Create author: 王浩羽 <br>
	    * Create on:Jan 6, 2009  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	private void receiveAddedOccupationCharge(Long occupationChargeItemDOId, Long identityValidateId){
		OccupationChargeItemDO occupationChargeItemDO= occupationChargeItemDao.findById(occupationChargeItemDOId);
		occupationChargeItemDO.setPayState(SysrefcodeDO.OM_FEE_STATE_IN);//已收
		//occupationChargeItemDao.updateObject(occupationChargeItemDO);
		
		//如果全部费用项目都已经缴费，那么通知认证已经入账
		List<OccupationChargeItemDO> list = occupationChargeItemDao.queryItemListByIdentityValidateId(identityValidateId);
		if(BlankUtil.isBlank(list))	{
			return;
		}
		for(OccupationChargeItemDO chargeItemDO : list) {
			// 要跟进的
			if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(chargeItemDO.getTrackYn())){
				// 有一个未收则返回
				if(SysrefcodeDO.OM_FEE_STATE_NO.equals(chargeItemDO.getPayState())){
					return ;
				}
			}
		}
		identityValidateService.receiveAddedOccupationCharge(identityValidateId);
	}
	
	/**
	 * 功能描述:是否允许处理收费项目  <br>
	 * @param chargeItemDOId
	 * @return
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 12, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private boolean isAllowDeleteOccupationChargeItem(Long chargeItemDOId ){
		return isChargeItemTrack(chargeItemDOId);
	}
	
	/**
	 * 功能描述: 判断收费项目是否是跟进的 <br>
	 * @param chargeItemDOId
	 * @return
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 12, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private boolean isChargeItemTrack(Long chargeItemDOId){
		OccupationChargeItemDO chargeItemDO= occupationChargeItemDao.findById(chargeItemDOId);
		// 跟进
		if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(chargeItemDO.getTrackYn())) {
			return false;
		}
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.IAbstractChargeRecordService#monthBalance(com.ce.pms.renovation.vo.ChargeRecordVO)
	 */
	public ChargeRecordVO monthBalance(ChargeRecordVO chargeRecordVO) {
		throw new BusinessException("occupation:occupation_chargeRecordProcessor_monthBalance_notSupport_exception", BusinessException.WARNING);
	}

}
